import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:tool/utils/GSpUtils.dart';

import '../../../resources.dart';
import '../../../base/base_model.dart';
import '../m/class_model.dart';

/// created by hcl
/// on 2025/5/5
/// HomeModel
/// desc
class TableModel extends BaseModel {
  double oneWidth = 120.ss;
  double oneHeight = 50.ss;
  List<String> weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  int weekCount = 7;
  int amCount = 4;
  int pmCount = 4;
  int nightCount = 2;
  TransformationController transformationController =
      TransformationController();
  Map<String, List> rowMap = {};

  bool showAdd = false;
  String addRowKey = '';
  int addColumn = 0;
  TextEditingController classController = TextEditingController();
  FocusNode classFocusNode = FocusNode();
  TextEditingController teacherController = TextEditingController();
  FocusNode teacherFocusNode = FocusNode();
  TextEditingController roomController = TextEditingController();
  FocusNode roomFocusNode = FocusNode();
  TextEditingController markController = TextEditingController();
  FocusNode markFocusNode = FocusNode();

  @override
  void initData() {
    // transformationController.value = Matrix4.identity()..scale(0.5);
    initConfig();
  }

  void initConfig() {
    Future.delayed(const Duration(milliseconds: 300), () {
      String tableConfig = GSpUtils.get(GSpUtils.tableConfig);
      if (tableConfig.isNotEmpty && tableConfig.contains('@')) {
        var splits = tableConfig.split('@');
        weekCount = int.tryParse(splits[0]) ?? 7;
        amCount = int.tryParse(splits[1]) ?? 4;
        pmCount = int.tryParse(splits[2]) ?? 4;
        nightCount = int.tryParse(splits[3]) ?? 2;
        notifyListeners();
      }
      initTable();
    });
  }

  void initTable() {
    rowMap.clear();
    initRow(amCount, 'am');
    initRow(pmCount, 'pm');
    initRow(nightCount, 'night');
    notifyListeners();
  }

  void initRow(int rowCount, String rowType) {
    for (int i = 0; i < rowCount; i++) {
      String rowKey = '$rowType${i + 1}';
      String rowStr = GSpUtils.get(rowKey);
      if (rowStr.isNotEmpty) {
        try {
          rowMap[rowKey] = json.decode(rowStr);
        } catch (e) {}
      }
      if (rowMap[rowKey] == null) {
        rowMap[rowKey] = ['', '', '', '', '', '', ''];
      }
    }
  }

  void addClass(String rowKey, int column) {
    showAdd = true;
    addRowKey = rowKey;
    addColumn = column;

    String classStr = GSpUtils.get('$addRowKey@$addColumn');
    if (classStr.isNotEmpty) {
      try {
        var classModel = ClassModel.fromMap(json.decode(classStr));
        classController.text = classModel.name;
        teacherController.text = classModel.teacher;
        roomController.text = classModel.room;
        markController.text = classModel.mark;
      } catch (e) {}
    }
    notifyListeners();
  }

  void closeShowAdd() {
    showAdd = false;
    addClear();
    notifyListeners();
  }

  void addSave() {
    if (classController.text.isEmpty) {
      showToast('请输入课程名称');
      return;
    }
    List tempRow = rowMap[addRowKey] ?? ['', '', '', '', '', '', ''];
    tempRow[addColumn] = classController.text;
    rowMap[addRowKey] = tempRow;
    GSpUtils.set(addRowKey, json.encode(tempRow));
    GSpUtils.set(
        '$addRowKey@$addColumn',
        json.encode(ClassModel(classController.text, teacherController.text,
                roomController.text, markController.text)
            .toMap()));
    closeShowAdd();
  }

  void addClear() {
    classController.text = '';
    teacherController.text = '';
    roomController.text = '';
    markController.text = '';
  }
}
